// pages/order/product-comment/product-comment.js
import api from '../../../utils/api'
import message from '../../../utils/message';
Page({

    /**
     * 页面的初始数据
     */
    data: {

        order_product_comment_list:[],
        user_comment_list:[],
        status:0,
        page_index:1,
        page_size:5,
        no_more_data:false,
        swiper_current: 0,
        init_user_comment:false

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.getOrderProductCommentList();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getOrderProductCommentList:function(  ){
        //加载已评价和待评价的订单商品列表
        var me = this;

        var params = {};
        params.status = this.data.status;
        params.page_index = this.data.page_index;
        params.page_size = this.data.page_size

        api.order.getOrderProductCommentList( params , function( response ){

            if(response.status==1){
                var len = response.data.length;
                if(len<5){
                    me.setData({
                        order_product_comment_list:response.data,
                        no_more_data:true
                    })
                }else{
                    me.setData({
                        order_product_comment_list:response.data
                    })
                }
            }else{
                message.alertError(response.message)
            }
        })
    },
    selectItem:function( e ){
        var status = e.currentTarget.dataset.index;
        this.setData({
            status:status,
            page_index:1,
            swiper_current:status

        })

        if(status==1){
            //加载待评论

            if(this.data.init_user_comment==false){
                this.getUserProductCommentList();
            }
        }

        
    },

    swiperChange: function (e) {
        console.log(e);
        var page = this;
        page.setData({
            swiper_current: e.detail.current,
            status:e.detail.current
        });
    },
    getMore:function(){
        var page_index = this.data.page_index;
        page_index++;

        this.setData({
            page_index:page_index
        })
        var me = this;
        var params = {};
        params.status = this.data.status;
        params.page_index = this.data.page_index;
        params.page_size = this.data.page_size

        api.order.getOrderProductCommentList( params , function( response ){

            if(response.status==1){
                var len = response.data.length;

                if(len>0){

                    var list = me.data.order_product_comment_list;
                    var list2 = list.concat( response.data )
                    if(len<5){
                        me.setData({
                            order_product_comment_list:list2,
                            no_more_data:true
                        })
                    }else{
                        me.setData({
                            order_product_comment_list:list2
                        })
                    }

                }else{

                    me.setData({
                        no_more_data:true
                    })

                }
                
            }else{
                message.alertError(response.message)
            }
        })
    },
    getUserProductCommentList:function(){
        var me = this;
        api.user.getUserProductCommentList({} , function( response ){
            console.log(response);
            if(response.status ==1){


                var data = response.data;
                var len = data.length;
                for(var i=0;i<len;i++){
                    if(JSON.parse( data[i].images )){
                        data[i].images = JSON.parse( data[i].images )

                    }else{
                        data[i].images = [];
                    }
                }

                me.setData({
                    user_comment_list:data,
                    init_user_comment:true
                })
            }else{

                message.alertError( response.data )

            }
        })
    }
})